package com.ksp.api;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

public class SysLog {

	public static Logger log = Logger.getLogger(SysLog.class);

	/**
	 * 打印警告
	 * 
	 * @param obj
	 */
	public static void warn(Object obj) {
		try{
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
					+ "(" + stacks[2].getLineNumber() + ")";
			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
				String str = sw.toString();
				log.warn(location + str);
			} else {
				log.warn(location + obj.toString());
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 打印信息
	 * 
	 * @param obj
	 */
	public static void info(Object obj) {
		try{
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
					+ "(" + stacks[2].getLineNumber() + ")";
			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
				String str = sw.toString();
				log.info(location + str);
			} else {
				log.info(location + obj.toString());
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 打印错误
	 * 
	 * @param obj
	 */
	public static void error(Object obj) {
		try{
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
					+ "(" + stacks[2].getLineNumber() + ")";

			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				StringWriter sw = new StringWriter();
				e.printStackTrace(new PrintWriter(sw, true));
				String str = sw.toString();
				log.error(location + str);
			} else {
				log.error(location + obj.toString());
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 向数据库告警表中插入信息
	 * @param obj
	 */
	public static void dbWarn(Object obj) {
		try{
			String printInfo = "";
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
					+ "(" + stacks[2].getLineNumber() + ")";

			/*** 是否是异常  ***/
			if (obj instanceof Exception) {
				Exception e = (Exception) obj;
				printInfo = location + e.getMessage();
				log.fatal(printInfo.substring(0, printInfo.length() > 512?512:printInfo.length()));
			} else {
				printInfo = location + obj.toString();
				log.fatal(printInfo.substring(0, printInfo.length() > 512?512:printInfo.length()));
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 获取调用此函数的代码的位置
	 * @return 包名.类名.方法名(行数)
	 */
	public static String getCodeLocation(){
		try{
			/*** 获取输出信息的代码的位置 ***/
			String location = "";
			StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
			location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
					+ "(" + stacks[2].getLineNumber() + ")";
			return location;
		}catch (Exception e) {
			// TODO: handle exception
			SysLog.error(e);
			return "";
		}
	}
}
